package com.ffdms.service;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import com.ffdms.mobileoffice.MessageActivity;
import com.ffdms.mobileoffice.PlanListActivity;
import com.ffdms.mobileoffice.R;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Message;
import android.os.Parcel;
import android.os.RemoteException;
import android.util.Log;
import android.widget.RemoteViews;

/**
 * ϵͳ��ʹ����
 * */
public class MessagerService extends Service {

	private static String TAG = "Messager";
	private static long GET_CHAT_MESSAGE_PERIOD = 29 * 1000;//29 seconds
	private static long GET_BULLETIN_MESSAGE_PERIOD = 60 * 1000;//1 minutes
	public static final int CHAT_MESSAGE = 0;
	public static final int BULLETIN_MESSAGE = 1;
	private Timer chatTimer;
	private Timer bulletinTimer;
	private NotificationManager mNotificationManager;
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		
		chatTimer = new Timer();
		bulletinTimer = new Timer();
		ChatTimerTask ctt = new ChatTimerTask();
		chatTimer.schedule(ctt, 0, GET_CHAT_MESSAGE_PERIOD);
		BulletinTimerTask btt = new BulletinTimerTask();
		bulletinTimer.schedule(btt, 0, GET_BULLETIN_MESSAGE_PERIOD);

		mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Log.d(TAG, "onCreate");
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		chatTimer.cancel();
		bulletinTimer.cancel();
		Log.d(TAG, "onDestroy");
	}

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
		Log.d(TAG, "onStart");
	}

	@Override
	public boolean onUnbind(Intent intent) {
		// TODO Auto-generated method stub
		return super.onUnbind(intent);
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		Log.d(TAG, "onBind");
		return mBinder;
	}

	private final IBinder mBinder = new Binder() {
        @Override
        protected boolean onTransact(int code, Parcel data, Parcel reply,
                int flags) throws RemoteException {
            return super.onTransact(code, data, reply, flags);
        }
    };
    
    private void showNotification(int layoutId, int iconId, CharSequence title, CharSequence content, Class clazz)
	{
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, clazz), 0);
		Notification notif = new Notification(iconId, title,
                System.currentTimeMillis());
		notif.setLatestEventInfo(this, title,content, contentIntent);
        mNotificationManager.notify(layoutId, notif);
	}
    
	public class ChatTimerTask extends TimerTask {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			Log.d("ChatTimerTask", "Occur at:" + System.currentTimeMillis());
			String json = "";//json from net, and parse json to object
			com.ffdms.vo.Message msg = new com.ffdms.vo.Message();
			msg.setTitle("msg title " + System.currentTimeMillis());
			msg.setDescription("msg description " + System.currentTimeMillis());
			msg.setAddTime(new Date().toString());
			msg.setSender(1);
			msg.setEnterpriseID(1);
//			msg.setiD(1);
			msg.setModifyTime(new Date().toString());
			msg.setSendTime(new Date().toString());
			msg.setStatus(1);
			
//			Message m = new Message();
//			m.what = CHAT_MESSAGE;
//			m.obj = msg; 
//			mHandler.sendMessage(m);

			MessageService msgSvc = new MessageService(getApplicationContext(), null);
			msgSvc.addMessage(msg);
			
//			showNotification(R.layout.activity_message, 
//					R.drawable.noti_ico, 
//					msg.getTitle(), 
//					msg.getDescription(),
//					MessageActivity.class);
		}

	}

	public class BulletinTimerTask extends TimerTask {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			Log.d("BulletinTimerTask", "Occur at:" + System.currentTimeMillis());
			
			String json = "";//json from net, and parse json to object
			com.ffdms.vo.Notice notice = new com.ffdms.vo.Notice();
			notice.setTitle("notice title "+ System.currentTimeMillis());
			notice.setDescription("notice description "+ System.currentTimeMillis());
			notice.setAddTime(new Date().toString());
			notice.setEnterpriseID(1);
//			notice.setiD(1);
			notice.setModifyTime(new Date().toString());
			
			BulletinService svc = new BulletinService(getApplicationContext(), null);
			svc.addNotice(notice);
			
//			showNotification(R.layout.activity_plan, 
//					R.drawable.noti_ico, 
//					notice.getTitle(), 
//					notice.getDescription(),
//					PlanActivity.class);
		}

	}
}
